﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using ConfApp.Models;
using ConfApp.Services;
using Microsoft.Phone.Net.NetworkInformation;

namespace ConfApp.ViewModels
{
    public class SpeakerInfoViewModel : ViewModel 
    {
        private readonly Speaker _speaker;
        
        public SpeakerInfoViewModel(
            Speaker speaker, 
            INavigationService navigationService)
            : base(navigationService)
        {
            _speaker = speaker;
        }

        public int ID { get { return _speaker.ID; } }
        public string FullName { get { return _speaker.FullName.Replace(" ",System.Environment.NewLine); } }
        public string FirstName { get { return _speaker.FullName.Split(' ')[0]; } }
        public string LastName { get { return _speaker.FullName.Split(' ')[1]; } }
        public string Employer { get { return _speaker.Employer; } }
        public Speaker Speaker { get { return _speaker; } }
        public BitmapImage Picture
        {
            get
            {
                if (Speaker.Picture != null)
                {
                    var image = new BitmapImage();
                    image.SetSource(new MemoryStream(Speaker.Picture));
                    return image;
                }
                var fileName = System.IO.Path.GetFileName(Speaker.QualifiedUrl);
                var sr = Application.GetResourceStream(new Uri(@"Resources/SpeakerPictures/" + fileName, UriKind.Relative));
                if (sr != null)
                {
                    return new BitmapImage(new Uri(@"\Resources\SpeakerPictures\" + fileName, UriKind.RelativeOrAbsolute));
                }
                if (NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None)
                {
                    return new BitmapImage(new Uri(Speaker.QualifiedUrl, UriKind.RelativeOrAbsolute));
                }
                return new BitmapImage(new Uri(@"\Resources\SpeakerPictures\no-pic.png", UriKind.RelativeOrAbsolute));
            }
        }
        
        //public string PictureUrl { get { return _speaker.PictureUrl; } }

        public override void IsBeingActivated()
        {

        }
    }
}
